#include "hplcsparklinkfindpacket.h"
#include "hplcsparklinkpacketformat.h"
#include "hplcsparklinkautosamplerpfcid.h"
#include "common/convert.h"
#include "common/misc.h"
#include "common/str.h"
#include <QDebug>

namespace hplc
{
namespace sparklink
{

HplcSparkLinkFindPacket::HplcSparkLinkFindPacket(QObject *parent)
    : BaseFindPacket (parent)
{

}

HplcSparkLinkFindPacket::~HplcSparkLinkFindPacket()
{

}

QList<ProtocolPacket> HplcSparkLinkFindPacket::find(const QByteArray &data)
{
    m_rxBuffer.append(data);

    quint32 cmdSize = 0;
    char _data = 0;

    QList<ProtocolPacket>   packets;
    while (!m_rxBuffer.isEmpty())
    {
        _data = m_rxBuffer.at(0);
        m_rxBuffer.remove(0, 1);//移除头字节

        if (_data == SPARKLINK_PROTOCOL_ACK
                || _data == SPARKLINK_PROTOCOL_NACK
                || _data == SPARKLINK_PROTOCOL_NACK0)//接收到回复信号则删除当前命令
        {
            // 如果之前出现过错误包
            if (m_isHadErrorBytes)
            {
               m_statistics->m_current.m_receivedErrorPackets++;
            }
            m_isHadErrorBytes = false;

            m_rxPacket.append(_data); // NACK和ACK不转换，后面识别时，包大小是1

            m_framePos = 0;
            m_statistics->m_current.m_receivedCorrectBytes++;
            m_statistics->m_current.m_receivedCorrectPackets++;

            // 一包回复包
            ProtocolPacket packet;
            packet.timestamp = Misc::getTimeStamp();
            packet.buffer = m_rxPacket;
            packets << packet;
            m_rxPacket.clear();
        }
        else if (_data == SPARKLINK_PROTOCOL_STX)
        {
            // 如果之前出现过错误包
            if (m_isHadErrorBytes)
            {
               m_statistics->m_current.m_receivedErrorPackets++;
            }

            m_framePos = 0;
            m_rxBufferAscii.clear();
            m_rxPacket.clear();
        }
        else if (m_framePos == 0 && _data != SPARKLINK_PROTOCOL_STX)
        {
            m_isHadErrorBytes = true;
            continue;
        }

        if (m_framePos++ < SPARKLINK_PROTOCOL_MAX_LEN)
        {
            m_rxBufferAscii.append(_data);
        }
        else // 包长度过长
        {
            qDebug() << "The cmd is too long";
            m_framePos = 0;
            m_isHadErrorBytes = true;
            emit errorPacket(ERROR_TOO_LONG);
            continue;
        }

        if (_data == SPARKLINK_PROTOCOL_ETX)
        {
            if (m_framePos != SPARKLINK_PROTOCOL_MAX_LEN)
            {
                qDebug() << "The cmd size is error : " << m_framePos;
                m_framePos = 0;
                m_isHadErrorBytes = true;
                emit errorPacket(ERROR_TOO_LONG);
                continue;
            }

            // 如果之前出现过错误包，这里不用统计，因为前面HEAD已经统计过了
            m_isHadErrorBytes = false;

            cmdSize = m_framePos / 2 - 1;

            m_rxPacket = m_rxBufferAscii.mid(1, m_rxBufferAscii.length() - 2);

//			qDebug("The cmd is: ");
//			for(quint32 i = 0; i < cmdSize; i++)
//			{
//			    qDebug("%02X ", m_rxPacket.at(i));
//			}
//			qDebug("\r\n");

            m_framePos = 0;

            m_statistics->m_current.m_receivedCorrectBytes += m_framePos;
            m_statistics->m_current.m_receivedCorrectPackets++;

            // 一包普通的包
            ProtocolPacket packet;
            packet.timestamp = Misc::getTimeStamp();
            packet.buffer = m_rxPacket;
            packets << packet;
            m_rxPacket.clear();
        } // if tail
    } // while

    return packets;
}

} // sparklink
} // hplc
